/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.service.wms;

import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import chunmap.model.elem.Envelope;
import chunmap.view.layer.Layer;

public class Capabilities {

	private static final String version = "1.1.1";

	private String baseURL;
	private String link;
	private String serviceTile;
	private String url;
	private String srs;
	private List<Layer> layers=new ArrayList<Layer>();

	public void reponse(PrintStream out) throws IOException {
		getHead(out);
		for (Layer layer : layers) {
			getBody(out, layer);
		}
		getFoot(out);
	}

	private void getHead(PrintStream out) throws IOException {
		out.println("<?xml version='1.0' encoding=\"UTF-8\" standalone=\"no\" ?>");
		out.println("<!DOCTYPE WMT_MS_Capabilities SYSTEM \"" + baseURL
				+ "/DTD?VERSION=" + version + "\">");
		out.println("<WMT_MS_Capabilities version=\"" + version + "\">");
		out.println("<Service>");
		out.println("  <Name>OGC:WMS</Name>");
		out.println("  <Title>" + serviceTile + "</Title>");
		out.println("  <OnlineResource xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\""
						+ link + "\" />");
		out.println("  <Fees>none</Fees>");
		out.println("  <AccessConstraints>none</AccessConstraints>");
		out.println("</Service>");
		out.println("<Capability>");
		out.println("  <Request>");
		out.println("    <GetCapabilities>");
		out.println("      <Format>application/vnd.ogc.wms_xml</Format>");
		out.println("      <DCPType>");
		out.println("        <HTTP>");
		out.println("          <Get>");
		out.println("            <OnlineResource xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\""
						+ url + "\"/>");
		out.println("          </Get>");
		out.println("        </HTTP>");
		out.println("      </DCPType>");
		out.println("    </GetCapabilities>");
		out.println("    <GetMap>");
		out.println("      <Format>image/jpg</Format>");
		out.println("      <Format>image/png</Format>");
		out.println("      <DCPType>");
		out.println("        <HTTP>");
		out.println("          <Get>");
		out.println("            <OnlineResource xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\""
						+ url + "\"/>");
		out.println("          </Get>");
		out.println("        </HTTP>");
		out.println("      </DCPType>");
		out.println("    </GetMap>");
		out.println("    <GetFeatureInfo>");
		out.println("      <Format>text/plain</Format>");
		out.println("      <Format>text/html</Format>");
		out.println("      <DCPType>");
		out.println("        <HTTP>");
		out.println("          <Get>");
		out.println("            <OnlineResource xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\""
						+ url + "\"/>");
		out.println("          </Get>");
		out.println("        </HTTP>");
		out.println("      </DCPType>");
		out.println("    </GetFeatureInfo>");
		out.println("  </Request>");
		out.println("  <Exception>");
		out.println("    <Format>application/vnd.ogc.se_xml</Format>");
		out.println("  </Exception>");
		out.println(" <Layer>");
		out.println("    <Title>" + serviceTile + "</Title>");
		out.println("    <SRS>" + srs + "</SRS>");
	}

	private void getBody(PrintStream out, Layer layer)
			throws IOException {
		out.println("  <Layer>");
		out.println("    <Name>" + layer.getName() + "</Name>");
		out.println("      <Title>" + layer.getName() + "</Title>");
		Envelope env = layer.getEnvelop();
		out.println("      <LatLongBoundingBox minx=\"" + env.getMinX()
				+ "\" miny=\"" + env.getMinY() + "\" maxx=\"" + env.getMaxX()
				+ "\" maxy=\"" + env.getMaxY() + "\" />");

		// do not support style
		// out.println("      <Style>");
		// out.println("        <Name>" + "?" + "</Name>");
		// out.println("        <Title>" + "?" + "</Title>");
		// out.println("      </Style>");

		out.println("  </Layer>");
	}

	private void getFoot(PrintStream out) throws IOException {
		out.println(" </Layer>");
		out.println("</Capability>");
		out.println("</WMT_MS_Capabilities>");
	}

	// ------------------------------------------------------------------------------
	/**
	 * @return the baseURL
	 */
	public String getBaseURL() {
		return baseURL;
	}

	/**
	 * @param baseURL
	 *            the baseURL to set
	 */
	public void setBaseURL(String baseURL) {
		this.baseURL = baseURL;
	}

	/**
	 * @return the link
	 */
	public String getLink() {
		return link;
	}

	/**
	 * @param link
	 *            the link to set
	 */
	public void setLink(String link) {
		this.link = link;
	}

	/**
	 * @return the serviceTile
	 */
	public String getServiceTile() {
		return serviceTile;
	}

	/**
	 * @param serviceTile
	 *            the serviceTile to set
	 */
	public void setServiceTile(String serviceTile) {
		this.serviceTile = serviceTile;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the srs
	 */
	public String getSrs() {
		return srs;
	}

	/**
	 * @param srs
	 *            the srs to set
	 */
	public void setSrs(String srs) {
		this.srs = srs;
	}

	/**
	 * @return the layers
	 */
	public List<Layer> getLayers() {
		return layers;
	}

	/**
	 * @param layers
	 *            the layers to set
	 */
	public void setLayers(List<Layer> layers) {
		this.layers = layers;
	}
}
